//
//  HXCustomNavigationController.m
//  照片选择器
//
//  Created by 洪欣 on 2017/10/31.
//  Copyright © 2017年 洪欣. All rights reserved.
//

#import "HXCustomNavigationController.h"
#import "HXAlbumListViewController.h"
#import "HXDatePhotoViewController.h"
#import "HXPhotoTools.h"

@interface HXCustomNavigationController ()<HXAlbumListViewControllerDelegate, HXDatePhotoViewControllerDelegate>

@end

@implementation HXCustomNavigationController
- (instancetype)initWithManager:(HXPhotoManager *)manager {
    return [self initWithManager:manager delegate:nil doneBlock:nil cancelBlock:nil];
}
- (instancetype)initWithManager:(HXPhotoManager *)manager
                       delegate:(id<HXCustomNavigationControllerDelegate>)delegate {
    return [self initWithManager:manager delegate:delegate doneBlock:nil cancelBlock:nil];
}
- (instancetype)initWithManager:(HXPhotoManager *)manager
                      doneBlock:(viewControllerDidDoneBlock)doneBlock
                    cancelBlock:(viewControllerDidCancelBlock)cancelBlock {
    return [self initWithManager:manager delegate:nil doneBlock:doneBlock cancelBlock:cancelBlock];
}
- (instancetype)initWithManager:(HXPhotoManager *)manager
                       delegate:(id<HXCustomNavigationControllerDelegate>)delegate
                      doneBlock:(viewControllerDidDoneBlock)doneBlock
                    cancelBlock:(viewControllerDidCancelBlock)cancelBlock {
    manager.selectPhotoing = YES;
    [manager selectedListTransformBefore];
    dispatch_async(manager.loadAssetQueue, ^{
        [manager preloadData];
    });
    
    if (manager.configuration.albumShowMode == HXPhotoAlbumShowModeDefault) {
        HXAlbumListViewController *vc = [[HXAlbumListViewController alloc] initWithManager:manager];
        self = [super initWithRootViewController:vc];
        if (self) {
            self.hx_delegate = delegate;
            self.manager = manager;
            self.doneBlock = doneBlock;
            self.cancelBlock = cancelBlock;
            vc.doneBlock = self.doneBlock;
            vc.cancelBlock = self.cancelBlock;
            vc.delegate = self;
            
        }
    }else if (manager.configuration.albumShowMode == HXPhotoAlbumShowModePopup) {
        HXDatePhotoViewController *vc = [[HXDatePhotoViewController alloc] init];
        vc.manager = manager;
        self = [super initWithRootViewController:vc];
        if (self) {
            self.hx_delegate = delegate;
            self.manager = manager;
            self.doneBlock = doneBlock;
            self.cancelBlock = cancelBlock;
            vc.doneBlock = self.doneBlock;
            vc.cancelBlock = self.cancelBlock;
            vc.delegate = self;
        }
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UINavigationBar *bar = self.navigationBar;
    //1.可以设置背景色
    [bar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];//去除导航栏黑线
    [bar setShadowImage:[UIImage new]];
    //2.可以设置背景图
    //        bar.barTintColor = [UIColor whiteColor];
    bar.tintColor = [UIColor whiteColor];
    //4.可以设置标题文字的垂直位置
    [bar setTitleVerticalPositionAdjustment:0 forBarMetrics:UIBarMetricsDefault];
    //5.设置导航栏的样式－－影响状态栏中文字的颜色
    [bar setBarStyle:UIBarStyleDefault];
    //6.可以设置标题栏文字的样式
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
    attributes[NSForegroundColorAttributeName] = [UIColor blackColor];
    [bar setTitleTextAttributes:attributes];
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.viewControllers.count > 0) {
        //        viewController.navigationItem.hidesBackButton = YES;
        //        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationbar_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popAction)];
        //        viewController.navigationItem.leftBarButtonItem = backBarButtonItem;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(0, 0, 44, 44);
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.backgroundColor = [UIColor clearColor];
        [btn setImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
        viewController.navigationItem.hidesBackButton = YES;
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        //        backItem.title = backTitle;
        viewController.navigationItem.leftBarButtonItem = backItem;
        
    }
    
    [super pushViewController:viewController animated:animated];
    
}

- (void)popAction{
    [self popViewControllerAnimated:YES];
}

#pragma mark - < HXAlbumListViewControllerDelegate >
- (void)albumListViewControllerDidCancel:(HXAlbumListViewController *)albumListViewController {
    if ([self.hx_delegate respondsToSelector:@selector(photoNavigationViewControllerDidCancel:)]) {
        [self.hx_delegate photoNavigationViewControllerDidCancel:self];
    }
}
- (void)albumListViewController:(HXAlbumListViewController *)albumListViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
    if ([self.hx_delegate respondsToSelector:@selector(photoNavigationViewController:didDoneAllList:photos:videos:original:)]) {
        [self.hx_delegate photoNavigationViewController:self didDoneAllList:allList photos:photoList videos:videoList original:original];
    }
}
#pragma mark - < HXDatePhotoViewControllerDelegate >
- (void)datePhotoViewControllerDidCancel:(HXDatePhotoViewController *)datePhotoViewController {
    if ([self.hx_delegate respondsToSelector:@selector(photoNavigationViewControllerDidCancel:)]) {
        [self.hx_delegate photoNavigationViewControllerDidCancel:self];
    }
}
- (void)datePhotoViewController:(HXDatePhotoViewController *)datePhotoViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original {
    if ([self.hx_delegate respondsToSelector:@selector(photoNavigationViewController:didDoneAllList:photos:videos:original:)]) {
        [self.hx_delegate photoNavigationViewController:self didDoneAllList:allList photos:photoList videos:videoList original:original];
    }
}
- (BOOL)shouldAutorotate{
    if (self.isCamera) {
        return NO;
    }
    if (self.manager.configuration.supportRotation) {
        return YES;
    }else {
        return NO;
    }
}

//支持的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.isCamera) {
        return UIInterfaceOrientationMaskPortrait;
    }
    if (self.manager.configuration.supportRotation) {
        return UIInterfaceOrientationMaskAll;
    }else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

- (void)dealloc {
    self.manager.selectPhotoing = NO;
    if (HXShowLog) NSSLog(@"dealloc");
}

@end
